<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Object.defineProperties</title>
</head>
<body>
    <script>
        //要求添加 total 属性, 获得团队的总伤害
        // total
        var team = {
            name: 'RNG',
            damages: [
                {
                    name: 'Bin',
                    damage: 14630
                },
                {
                    name: 'Wei',
                    damage: 9894
                },
                {
                    name: 'XiaoHu',
                    damage: 20202
                },
                {
                    name: 'Gala',
                    damage: 14819
                },
                {
                    name: 'Ming',
                    damage: 4397
                }
            ],
        };

        //为 team 添加 total 属性
        Object.defineProperty(team, 'total', {
            get: function(){
                // this 是指向当前操作的对象的
                //计算当前对象伤害的总值
                // var res = 0;
                // //循环
                // this.damages.forEach(function(item){
                //     res += item.damage
                // });
                // return res;

                //计算总和  reduce
                return this.damages.reduce(function(prev, item){
                    return prev + item.damage;
                }, 0);
            }
        });

        console.log(team.total);
        
    </script>
</body>

</html>